#pragma
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;


class netaddr
{
public:
    netaddr(const struct sockaddr_in &addr): _addr(addr)
    {
        // _ip = inet_ntoa(addr.sin_addr);
        char buffer[128];
        inet_ntop(AF_INET, &addr.sin_addr, buffer, sizeof(buffer));
        _ip = buffer;
        _port = ntohs(addr.sin_port);
    }
    string Ip() { return _ip; }
    uint16_t Poet() { return _port; }

    string SockInfo()
    {
        string info = _ip;
        info += ':';
        // 将网络字节序改成主机序列
        info += to_string(_port);

        return info;
    }

    struct sockaddr_in& GetAddr()
    {
        return _addr;
    }

    bool operator==(const netaddr& addr)
    {
        return _ip == addr._ip && _port == addr._port;
    }

    ~netaddr()
    {
    }

private:
    string _ip;
    uint16_t _port;

    struct sockaddr_in _addr;
};